/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.guicommon.widgets;

import java.util.LinkedList;
import java.util.List;

import net.miblounge.gradecalculator.core.enums.AbstractEnumType;
import net.miblounge.gradecalculator.core.enums.CourseType;
import net.miblounge.gradecalculator.core.enums.CourseTypeList;

import com.trolltech.qt.gui.QWidget;

public class CourseTypeSelector extends MultiComboBox {
	
	public CourseTypeSelector(final QWidget parent)
	{
		super(parent, getValues());
	}
	
	private static List<String> getValues()
	{
		final List<String> result = new LinkedList<String>();
		for (final CourseType type : CourseType.values()) {
			result.add(getText(type));
		}
		return result;
	}
	
	private static String getText(final AbstractEnumType type) {
		return type.getAbbreviation() + " - " + type.getName();
	}
	
	public CourseTypeList getCourseTypes()
	{
		final CourseTypeList result = new CourseTypeList(); 
		final List<String> selectedItems = super.getSelectedItems();
		for (final String item : selectedItems) {
			final CourseType type = CourseType.valueOf(item);
			if (type != null) {
				result.add(type);
			}
		}
		
		return result;
	}

	public void setCourseTypes(final CourseTypeList values) {
		final List<String> selectedItems = new LinkedList<String>();
		for (AbstractEnumType value : values) {
			selectedItems.add(getText(value));
		}
		
		super.setSelectedItems(selectedItems);
	}

}
